package com.njupt.Array;

/**
 * 滑动窗口
 */
public class MinSubArrayLen1 {

    public int minSubArrayLen(int target, int[] nums) {
        int left = 0;
        int right = 0;
        int sum = 0;
        int result = Integer.MAX_VALUE;
        for (right=0;right<nums.length;right++){
            sum += nums[right];
            while(sum>=target){
                result = Math.min(result,right-left+1);
                sum -= nums[left++];
            }
        }
        return result == Integer.MAX_VALUE?0:result;
    }

    public static void main(String[] args) {
        MinSubArrayLen1 test = new MinSubArrayLen1();
        int target = 11;
        int[] nums = {1,1,1,1,1,1,1,1};
        int result = test.minSubArrayLen(target, nums);
        System.out.println(result);
    }
}
